<?php
namespace Admin\Controller;
/**
 * 系统设置
 *
 * @package         YDCMS
 * @author          QQ:644157559 <644157559@qq.com>
 * @version         $Id: SystemController.class.php v1.0 2014-06-13 17:27:03 $
 */
class SystemController extends CommonController{
    private $model;
    private $service;
    private $logic;
    private $confName = 'web_config';
    //允许写入文件的配置项
    private $AllowConfig = array(
                "CFG_BASEHOST",
                "SITE_TITLE",
                "SITE_KEYWORD",
                "SITE_DESCRIPTION",
                "SITE_POWERBY",
                "CFG_SOFT_LANG",
                "HTML_DIR",
                "CMSPATH",
                "UPLOAD_PATH",
                "UPLOAD_MAX_SIZE",
                "UPLOAD_IMAGE_EXTS",
                "UPLOAD_DOCUMENT_EXTS",
                "THUMB_WIDTH",
                "THUMB_HEIGHT",
                "COOKIE_ENCODE",
                "CFG_AUOT_DESCRIPTION",
                "DELLINK",
                "SERVICE_SWITCH",
                "SERVICE_QQ",
                "SERVICE_WANGWANG",
                "URL_MODEL",
                "URL_HTML_SUFFIX",
                //"URL_PATHINFO_DEPR",
                "PAGE_SIZE",
                "ICP",
                "LANG_SWITCH_ON",
                "DEFAULT_LANG",
                "LANG_AUTO_DETECT",
                "DEFAULT_THEME",
                "INDEX_URL",
                "INDEX_NAME",
                "LIST_SYMBOL",
                "CFG_JUMP_ONCE",
                "SEARCH_TIME",
            );
    //需要重新分解成数组的配置
    private $_config_array = array(
        "SERVICE_WANGWANG", 
        "SERVICE_QQ",
        "UPLOAD_IMAGE_EXTS",
        "UPLOAD_DOCUMENT_EXTS"
    );
    private $_config_int = array(
        "SEARCH_TIME", 
        "PAGE_SIZE",
        "THUMB_WIDTH",
        "THUMB_HEIGHT",
        "CFG_AUOT_DESCRIPTION",
        "URL_MODEL",
    );
    
    private $cfg_photo_type;
    private $cfg_photo_typenames;
    private $cfg_photo_support;
    public function _initialize() {
        parent::_initialize();
        $this->model = D("System");
        $this->service = D("System", "Service");
        $this->logic = D("System", "Logic");
        $this->cfg_photo_type['gif'] = FALSE;
        $this->cfg_photo_type['jpeg'] = FALSE;
        $this->cfg_photo_type['png'] = FALSE;
        $this->cfg_photo_type['wbmp'] = FALSE;
        $this->cfg_photo_typenames = Array();
        $this->cfg_photo_support = '';
        if(function_exists("imagecreatefromgif") && function_exists("imagegif")){
            $this->cfg_photo_type["gif"] = TRUE;
            $this->cfg_photo_typenames[] = "image/gif";
            $this->cfg_photo_support .= "GIF ";
        }
        if(function_exists("imagecreatefromjpeg") && function_exists("imagejpeg")){
            $this->cfg_photo_type["jpeg"] = TRUE;
            $this->cfg_photo_typenames[] = "image/pjpeg";
            $this->cfg_photo_typenames[] = "image/jpeg";
            $this->cfg_photo_support .= "JPEG ";
        }
        if(function_exists("imagecreatefrompng") && function_exists("imagepng")){
            $this->cfg_photo_type["png"] = TRUE;
            $this->cfg_photo_typenames[] = "image/png";
            $this->cfg_photo_typenames[] = "image/xpng";
            $this->cfg_photo_support .= "PNG ";
        }
        if(function_exists("imagecreatefromwbmp") && function_exists("imagewbmp")){
            $this->cfg_photo_type["wbmp"] = TRUE;
            $this->cfg_photo_typenames[] = "image/wbmp";
            $this->cfg_photo_support .= "WBMP ";
        }
    }
    
    //系统设置首页
    public function index() {
        $act = I("get.act");
        if($act == 'make_encode'){//生成COOKIE_ENCODE
            echo genid();
            exit;
        }
        $conf = fast_cache($this->confName, '', YDCMS_DATA);
        $lang_list = D("Language", "Service")->getAll();
        $this->assign("lang_list", $lang_list);
        //var_dump($conf);die;
        $this->assign($conf);
        $this->display();
    }
    
    /**
     * 系统配置更新
     */
    public function ConfigUpdate() {
        $tmp_config = I("post.config");
        if(!isArrayAndNotEmpty($tmp_config)){
            return $this->errorReturn("无效操作！");
        }
        $config = array();
        foreach ($tmp_config as $key => $value) {
            if(in_array($key, $this->AllowConfig)){
                if(in_array($key, $this->_config_array) && trim($value) != ""){
                    $config[$key] = explode(",", $value); 
                }else if(in_array($key, $this->_config_int)){
                    $config[$key] = intval($value); 
                }else{
                    switch ($value) {
                        case "true":
                            $value = true;
                            break;
                        case "false":
                            $value = false;
                            break;
                        case "null":
                            $value = null;
                            break;

                        default:
                            break;
                    }
                    if($key == "LANG_SWITCH_ON"){
                        $config["HOME_LANG_SWITCH_ON"] = $value; 
                    }
                    $config[$key] = $value; 
                }
            }
        }
        $web_conf = fast_cache($this->confName, '', YDCMS_DATA);
        $url = "";
        
        $new_config = array_merge($web_conf, $config);
        if(false === (fast_cache($this->confName, $new_config, YDCMS_DATA))){
            return $this->errorReturn('系统信息设置失败！');
        }
        /*if($web_conf["URL_PATHINFO_DEPR"] != $config['URL_PATHINFO_DEPR']){
            $url = "/admin";
        }*/
        return $this->successReturn('系统信息设置成功！', $url);
    }
    //文档自定义属性管理
    public function conatt() {
        $M = M("Conatt");
        $list = $M->order('sortid asc')->select();
        //var_dump($list);die;
        $this->assign("list", $list);
        $this->display();
    }
    
    /**
     * 更新文档自定义属性管理
     */
    public function upConAtt() {
        $att = I("post.att");
        if(!isArrayAndNotEmpty($att) || !isArrayAndNotEmpty($att['att']) || !isArrayAndNotEmpty($att['attname']) || !isArrayAndNotEmpty($att['sortid']) || !(count($att['att']) == count($att['attname']) && count($att['att']) == count($att['sortid']))){
            return $this->errorReturn("无效操作！");
        }
        $result = $this->service->upConAtt($att);
        if(!$result["status"]){
            return $this->errorReturn($result["data"]['error']);
        }
        return $this->successReturn('更新成功！', U('conatt'));
    }
    
    public function sys_safe() {
        $act = I("post.act", "");
        $safe_conf = fast_cache("inc_safe_config", '', YDCMS_DATA . "safe/");
        if($act == "save"){
            $code_config = I("post.code");
            $safe_conf['code_gdopen'] = empty($code_config['code_gdopen']) ? "" : implode(",", $code_config['code_gdopen']);
            $safe_conf['code_type'] = empty($code_config['code_type']) ? 0 : intval($code_config['code_type']);
            $safe_conf['code_useImgBg'] = empty($code_config['code_useImgBg']) ? false : true;
            $safe_conf['code_useCurve'] = empty($code_config['code_useCurve']) ? false : true;
            $safe_conf['code_useNoise'] = empty($code_config['code_useNoise']) ? false : true;
            $safe_conf['code_width'] = empty($code_config['code_width']) ? 0 : intval($code_config['code_width']);
            $safe_conf['code_height'] = empty($code_config['code_height']) ? 0 : intval($code_config['code_height']);
            $safe_conf['code_length'] = intval($code_config['code_length']) <= 0 ? 5 : intval($code_config['code_length']);
            $safe_conf['code_fontsize'] = intval($code_config['code_fontsize']) <= 0 ? 12 : intval($code_config['code_fontsize']);
            if(false === (fast_cache("inc_safe_config", $safe_conf, YDCMS_DATA . "safe/"))){
                return $this->errorReturn('验证码参数设置失败！');
            }

            return $this->successReturn('验证码参数设置成功！');
        }else{
            $this->assign($safe_conf);
            $this->display("safe");
        }
    }
    
    public function sys_info_mark() {
        if($this->cfg_photo_support == ''){
            $this->error("你的系统没安装GD库，不允许使用本功能！");
        }
        $act = I("post.act", "");
        $mark_conf = fast_cache("inc_photowatermark_config", '', YDCMS_DATA . "mark/");
        if($act == "save"){
            $new_mark_conf = array();
            $new_mark_conf['photo_markimg'] = $mark_conf['photo_markimg'];
            $tmp_mark_conf = I("post.mark");
            $newimg = $_FILES['newimg'];
            if(is_uploaded_file($newimg['tmp_name'])){
                $imgfile_type = strtolower(trim($newimg['type']));
                if(!in_array($imgfile_type, $this->cfg_photo_typenames))
                {
                    $this->error("上传的图片格式错误，请使用 {$this->cfg_photo_typenames}格式的其中一种！");
                    exit();
                }
                if($imgfile_type=='image/xpng' || $imgfile_type=='image/png')
                {
                    $shortname = ".png";
                }
                else if($imgfile_type=='image/gif')
                {
                    $shortname = ".gif";
                }
                else
                {
                    $shortname = ".gif";
                }
                $photo_markimg = 'mark'.$shortname;
                @move_uploaded_file($newimg['tmp_name'], YDCMS_DATA . "mark/".$photo_markimg);
                $new_mark_conf['photo_markimg'] = $photo_markimg;
            }
            $new_mark_conf['photo_markup'] = $tmp_mark_conf['photo_markup'] == 1 ? 1 : 0;
            $new_mark_conf['photo_marktype'] = intval($tmp_mark_conf['photo_marktype']);
            $new_mark_conf['photo_fontsize'] = intval($tmp_mark_conf['photo_fontsize']);
            $new_mark_conf['photo_fontcolor'] = trim($tmp_mark_conf['photo_fontcolor']) == '' ? '0,0,0' : trim($tmp_mark_conf['photo_fontcolor']);
            $new_mark_conf['photo_watertext'] = trim($tmp_mark_conf['photo_watertext']) == '' ? 'YDCMS' : trim($tmp_mark_conf['photo_watertext']);
            $new_mark_conf['photo_waterpos'] = intval($tmp_mark_conf['photo_waterpos']) > 9 ? 0 : intval($tmp_mark_conf['photo_waterpos']);
            $new_mark_conf['photo_diaphaneity'] = intval($tmp_mark_conf['photo_diaphaneity']) > 100 ? 100 : intval($tmp_mark_conf['photo_diaphaneity']);

            if(false === (fast_cache("inc_photowatermark_config", $new_mark_conf, YDCMS_DATA . "mark/"))){
                return $this->errorReturn('图片水印设置失败！');
            }
            
            return $this->successReturn('图片水印设置成功！');
        }else{
            //var_dump($mark_conf);die;
            $this->assign('cfg_photo_support', $this->cfg_photo_support);
            $this->assign($mark_conf);
            $this->display("mark");
        }
    }
}
